﻿using DMS.Models;
using DMS.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;

namespace DMS
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //Groupdocs.Web.UI.Viewer.InitRoutes();
            Groupdocs.Web.UI.Viewer.SetRootStoragePath(Server.MapPath("~/UploadFiles/"));
        }

        //protected void Session_Start(object src, EventArgs e)
        //{
        //    if (Context.Session != null)
        //    {
        //        if (Context.Session.IsNewSession)//|| Context.Session.Count==0)
        //        {
        //            string sCookieHeader = Request.Headers["Cookie"];
        //            if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
        //            {
        //                //if (Request.IsAuthenticated)
        //                FormsAuthentication.SignOut();
        //                Response.Redirect("/login");
        //            }
        //        }
        //    }

        //}

        //protected void Session_End(object sender, EventArgs e)
        //{
        //    //Code that runs when a session ends. 
        //    //Note: The Session_End event is raised only when the sessionstate mode 
        //    //is set to InProc in the Web.config file. If session mode is set to StateServer
        //    //or SQLServer, the event is not raised. 
        //    Session.Clear();
        //}

        void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            var authCookie = HttpContext.Current.Request.Cookies[ConstKey.SignInToken];
            if (authCookie != null)
            {
                string encTicket = authCookie.Value;
                if (!String.IsNullOrEmpty(encTicket))
                {
                    var ticket = FormsAuthentication.Decrypt(encTicket);
                    var id = new UserIdentity(ticket);
                    //var userRoles = Roles.GetRolesForUser(id.Name);
                    var userRoles = Roles.GetRolesForUser(id.UserName);
                    var prin = new GenericPrincipal(id, userRoles);
                    HttpContext.Current.User = prin;
                }
            }
        }

        public override void Init()
        {
            this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
            base.Init();
        }
    }
}